//
//  ViewController.m
//  BYTreeViewDemo
//
//  Created by cuibaoyin on 16/1/7.
//  Copyright © 2016年 Bau-yin. All rights reserved.
//

#import "ViewController.h"
#import "BYTreeNodeData.h"
#import "BYTreeView.h"
#import "BYTreeNodeCellOne.h"
#import "BYTreeNodeCellTwo.h"
#import "BYTreeNodeCellThree.h"
#import "BYTreeNodeCell.h"

@interface ViewController ()<BYTreeViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    BYTreeView *testTree = [[BYTreeView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    testTree.treeDelegate = self;
    [self.view addSubview:testTree];
    
    NSDictionary *dic = @{@"title":@"节点00"};
    BYTreeNodeData *node00 = [[BYTreeNodeData alloc] initWithDataModel:dic];
    
    NSString *string = @"节点10";
    BYTreeNodeData *node10 = [[BYTreeNodeData alloc] initWithDataModel:string];
    string = @"节点11";
    BYTreeNodeData *node11 = [[BYTreeNodeData alloc] initWithDataModel:string];
    string = @"节点12";
    BYTreeNodeData *node12 = [[BYTreeNodeData alloc] initWithDataModel:string];
    [node00 insertChild:node10];
    [node00 insertChild:node11];
    [node00 insertChild:node12];
    
    dic = @{@"title":@"节点20"};
    BYTreeNodeData *node20 = [[BYTreeNodeData alloc] initWithDataModel:dic];
    dic = @{@"title":@"节点21"};
    BYTreeNodeData *node21 = [[BYTreeNodeData alloc] initWithDataModel:dic];
    [node10 insertChild:node20];
    [node10 insertChild:node21];
    
    dic = @{@"title":@"节点23"};
    BYTreeNodeData *node23 = [[BYTreeNodeData alloc] initWithDataModel:dic];
    dic = @{@"title":@"节点24"};
    BYTreeNodeData *node24 = [[BYTreeNodeData alloc] initWithDataModel:dic];
    [node11 insertChild:node23];
    [node11 insertChild:node24];
    
    NSNumber *number = [NSNumber numberWithInt:31];
    BYTreeNodeData *node31 = [[BYTreeNodeData alloc] initWithDataModel:number];
    number = [NSNumber numberWithInt:32];
    BYTreeNodeData *node32 = [[BYTreeNodeData alloc] initWithDataModel:number];
    [node23 insertChild:node31];
    [node23 insertChild:node32];
    
    testTree.nodeData = node00;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - BYTreeViewDelegate
- (CGFloat)tree:(BYTreeView *)tree heightForNodeData:(BYTreeNodeData *)nodeData
{
    if ([nodeData.dataModel isKindOfClass:[NSString class]])
    {
        return 60.0f;
    }
    else if ([nodeData.dataModel isKindOfClass:[NSDictionary class]])
    {
        return 40.0f;
    }
    else if ([nodeData.dataModel isKindOfClass:[NSNumber class]])
    {
        return 30.0f;
    }
    return 0.0f;
}

- (BYTreeNodeCell *)tree:(BYTreeView *)tree cellForNodeData:(BYTreeNodeData *)nodeData
{
    if ([nodeData.dataModel isKindOfClass:[NSString class]])
    {
        static NSString *CustomCellIdentifierOne = @"BYTreeNodeCellOne";
        BYTreeNodeCellOne * cell=  (BYTreeNodeCellOne*)[tree dequeueReusableCellWithIdentifier:CustomCellIdentifierOne];
        if (cell == nil)
        {
            NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"BYTreeNodeCellOne" owner:self options:nil] ;
            cell = [nib objectAtIndex:0];
        }
        return cell;
    }
    else if ([nodeData.dataModel isKindOfClass:[NSDictionary class]])
    {
        static NSString *CustomCellIdentifierTwo = @"BYTreeNodeCellTwo";
        BYTreeNodeCellTwo * cell=  (BYTreeNodeCellTwo*)[tree dequeueReusableCellWithIdentifier:CustomCellIdentifierTwo];
        if (cell == nil)
        {
            NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"BYTreeNodeCellTwo" owner:self options:nil] ;
            cell = [nib objectAtIndex:0];
        }
        return cell;
    }
    else
    {
        static NSString *CustomCellIdentifierTwo = @"BYTreeNodeCellThree";
        BYTreeNodeCellThree * cell=  (BYTreeNodeCellThree*)[tree dequeueReusableCellWithIdentifier:CustomCellIdentifierTwo];
        if (cell == nil)
        {
            NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"BYTreeNodeCellThree" owner:self options:nil] ;
            cell = [nib objectAtIndex:0];
        }
        return cell;
    }
}

- (void)tree:(BYTreeView *)tree didSelectNodeCell:(BYTreeNodeCell *)node
{
    NSLog(@"被选中的Model:%@",node.nodeData.dataModel);
}
@end
